package com.ricohgag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricohgag.entity.Goods;
import com.ricohgag.entity.GoodsCatalog;
import com.ricohgag.mapper.GoodsCatalogMapper;
import com.ricohgag.service.GoodsCatalogService;
import com.ricohgag.service.GoodsService;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Service
public class GoodsCatalogServiceImpl extends ServiceImpl<GoodsCatalogMapper, GoodsCatalog> implements GoodsCatalogService {
    @Autowired
    private GoodsCatalogMapper goodsCatalogMapper;
    @Autowired
    private GoodsService goodsService;

    @Override
    public List<GoodsCatalog> selectGoodsCatalog() throws Exception{

        return goodsCatalogMapper.selectList(null);
    }

    @Override
    public IPage<GoodsCatalog> selectCatalogPage(String name, Page<GoodsCatalog> page) throws Exception{
        QueryWrapper<GoodsCatalog> qw = new QueryWrapper<>();
        if(name != null){
            qw.like("name", name);
        }

        IPage<GoodsCatalog> goodsCatalogIPage = goodsCatalogMapper.selectPage(page, qw);
        Assert.assertSame(page, goodsCatalogIPage);

        return goodsCatalogIPage;
    }

    @Override
    public GoodsCatalog selectCatalogOne(Integer id) throws Exception{

        return goodsCatalogMapper.selectById(id);
    }

    @Override
    public int updateCatalog(GoodsCatalog goodsCatalog) throws Exception{
        return goodsCatalogMapper.updateById(goodsCatalog);
    }

    @Override
    public int insertCatalog(GoodsCatalog goodsCatalog) throws Exception{
        return goodsCatalogMapper.insert(goodsCatalog);
    }

    @Override
    public int deleteCatalog(int id) throws Exception{
        List<Goods> goodsList = goodsService.selectGoodsByCatalogId(id);

        for(Goods goods:goodsList){
            goodsService.deleteGoods(goods.getId());
        }

        int count = goodsCatalogMapper.deleteById(id);
        return count;
    }


}
